home *** CD-ROM | disk | FTP | other *** search
/ AppleScript - The Beta Release / AppleScript - The Beta Release.iso / Documentation / develop / Better Apple Event Coding / Code Samples / Application.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  5.8 KB  |  186 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------------------
  2.  
  3.     Program:    CPlusTESample 2.0AE
  4.     File:        Application.h
  5.     Uses:       AppLib.h
  6.  
  7.     by Andrew Shebanow
  8.     of Apple Macintosh Developer Technical Support
  9.     with modifications by Eric Berdahl
  10.  
  11.     Copyright © 1989-1990 Apple Computer, Inc.
  12.     Copyright © 1992 Eric Berdahl
  13.     All rights reserved.
  14.  
  15. ------------------------------------------------------------------------------------------*/
  16.  
  17. #ifndef __APPLICATION__
  18. #define __APPLICATION__
  19.  
  20. // Include necessary interface files
  21. #ifndef __STRING__
  22. #include <String.h>
  23. #endif
  24.  
  25. #ifndef __APPLEEVENTS__
  26. #include <AppleEvents.h>
  27. #endif
  28. #ifndef __TYPES__
  29. #include <Types.h>
  30. #endif
  31. #ifndef __DESK__
  32. #include <Desk.h>
  33. #endif
  34. #ifndef __EVENTS__
  35. #include <Events.h>
  36. #endif
  37. #ifndef __OSUTILS__
  38. #include <OSUtils.h>
  39. #endif
  40.  
  41. // we need resource ids
  42. #ifndef __APPLIB__
  43. #include "AppLib.h"
  44. #endif
  45.  
  46. // we need the exceptions code
  47. #ifndef __EXCEPTIONS__
  48. #include "Exceptions.h"
  49. #endif
  50.  
  51. class TDocument;
  52. class TDocumentList;
  53.  
  54. // YNCResult is a type used to indicate a result from
  55. // a user operation. YNC means yes/no/cancel, but yes
  56. // and no can also have synonyms like Save and Discard.
  57. enum YNCResult { yesResult, noResult, cancelResult };
  58.  
  59. /*
  60.     TApplication:
  61.  
  62.     This is our class which implements a basic Macintosh style program,
  63.     including a MultiFinder-aware event loop.
  64. */
  65.  
  66. // we derive from handle object to prevent fragmentation
  67. class TApplication {
  68. private:
  69.     static OSType fCreator;
  70.     static TApplication*    gApplication;
  71.  
  72. public:
  73.     static TApplication*    GetApplication();
  74.         // global accessor for the application
  75.  
  76.     // Our constructor
  77.     TApplication(OSType creator);
  78.  
  79.     // process finder arguments
  80.     virtual void    ProcessArgs();
  81.  
  82.     // Call this routine to start event loop running
  83.     // It will not return until the user quits the application (via a call
  84.     // to DoQuit, below)
  85.     virtual void    EventLoop();
  86.  
  87.     // Utility routines you can use
  88.     static Boolean            TrapAvailable(short tNumber,TrapType tType);
  89.         // Is trap implemented???
  90.     inline TDocumentList*    DocList() { return fDocList; };
  91.     static OSType            GetCreator() { return fCreator; };
  92.     static void                WDToDirID(short wdRefNum, short& vRefNum, long& dirID);
  93.         // utility routine to convert a working directory to a vRefNum/dirID pair
  94.  
  95. protected:
  96.     virtual long    StackNeeded() { return 0; };
  97.         // Returns total stack space required in bytes.
  98.         // Returns 0 by default, which tells the initialization code
  99.         // to use the default stack size.
  100.     virtual long    HeapNeeded() { return 0; };
  101.         // Returns total heap space required in bytes.
  102.         // Returns 0 by default, which tells the initialization code
  103.         // to use whatever heap size is given.
  104.  
  105.     // called by EventLoop to process an individual event
  106.     virtual void    ProcessEvent();
  107.  
  108.     // Loop control methods you may need to override
  109.     virtual void    SetUp() {};                // Run before event loop starts
  110.     virtual void    DoIdle() {};            // idle time handler (blink caret, background tasks)
  111.     virtual void    AdjustMenus() {};        // menu updater routine
  112.  
  113.     // event handlers you shouldn't need to override in a typical application
  114.     virtual void    DoOSEvent();
  115.         // Calls DoSuspend, DoResume and DoIdle as apropos
  116.     virtual void    DoMouseDown();
  117.         // Calls DoContent, DoGrow, DoZoom, etc
  118.     virtual void    DoKeyDown();
  119.         // also called for autokey events
  120.     virtual void    DoActivateEvt();
  121.         // handles setup, and calls DoActivate (below)
  122.     virtual void    DoUpdateEvt();
  123.         // handles setup, and calls DoUpdate (below)
  124.     virtual void    DoMouseInSysWindow() { SystemClick(&fTheEvent, fWhichWindow); };
  125.     virtual void    DoDrag();
  126.     virtual void    DoGoAway();
  127.         // handles setup, and calls TDocument::DoClose
  128.     virtual void    DoQuit(Boolean askUser, YNCResult defaultResult);
  129.  
  130.     // handlers you might override
  131.     virtual void    DoHighLevelEvent();
  132.  
  133.     // handlers you MUST override for functionality:
  134.     virtual void    AdjustCursor() = 0;
  135.         // cursor adjust routine, should setup mouseRgn
  136.     virtual void    DoMenuCommand(short menuID, short menuItem) = 0;
  137.     virtual void    DoNew() = 0;
  138.     virtual void    DoOpen() = 0;
  139.     virtual void    OpenADoc(short vRefNum, long dirID,
  140.                              StringPtr fName, OSType fType) = 0;
  141.  
  142.     // called by OSEvent (just calls DoActivate by default, so no clip conversion
  143.     // is done). If you want to convert clipboard, override these routines
  144.     virtual void    DoSuspend(Boolean doClipConvert);
  145.     virtual void    DoResume(Boolean doClipConvert);
  146.  
  147.     // If you have an app that needs to know about these, override them
  148.     virtual void    DoMouseUp() {};
  149.     virtual void    DoDiskEvt() {};
  150.  
  151.     // Utility routines you need to provide to do MultiFinder stuff
  152.     virtual unsigned long SleepVal() { return 0; };
  153.         // how long to sleep in WaitNextEvent
  154.  
  155.     // useful variables
  156.     Boolean            fDone;                    // set to true when we are ready to quit
  157.     EventRecord        fTheEvent;                // our event record
  158.     WindowPtr        fWhichWindow;            // currently active window
  159.     Boolean            fInBackground;            // true if our app is suspended
  160.     Boolean            fWantFrontClicks;        // true if we want front clicks
  161.     RgnHandle        fMouseRgn;                // mouse moved region (set it in your DoIdle)
  162.     TDocument*        fCurDoc;                // currently active document (if any)
  163.     TDocumentList*    fDocList;                // the list of documents
  164.  
  165.     static Boolean    fHaveWaitNextEvent;        // true if we have WaitNextEvent trap
  166.     static Boolean    fOnSystem7;                // running system 7 or better
  167. };
  168.  
  169. // some other handy utility routines, not actually a part of application class
  170.  
  171. // display alert, using specified error STR# resource and error code as index
  172. void AlertUser(short errResID, short errCode);
  173. // call AlertUser to display error message, then quit...
  174. void BigBadError(short errResID, short errCode);
  175. Boolean LookupErrorString(short value, short resID, StringPtr str);
  176.  
  177. // useful state checking Booleans (mostly for System 7 compatibility)
  178. extern Boolean gHaveColorQD;
  179.  
  180. inline void CopyPString(StringPtr to, StringPtr from)
  181. {
  182.     memcpy(to,from,(size_t) (from[0] & 0x0ff) + 1);
  183. }
  184.  
  185. #endif
  186.